/*
 * Copyright (c)  2020,  DataPipeline
 * All rights reserved.
 *
 * Id:BaseAttributeBuilder.java   2020-07-18 21:07 wanglijun
 */
package cn.datacare.orm.annotation.builder;

import cn.datacare.orm.database.TableDesc;
import cn.datacare.orm.dialect.AbstractDialect;
import cn.datacare.orm.manager.impl.SQLManager;
import cn.datacare.orm.mapping.BeanProcessor;
import cn.datacare.orm.mapping.type.JavaSqlTypeHandler;
import cn.datacare.orm.mapping.type.TypeParameter;
import cn.datacare.orm.util.Separator;

import java.beans.PropertyDescriptor;
import java.lang.annotation.Annotation;
import java.sql.SQLException;
import java.util.Map;

/**
 * <p>
 * Title: BaseAttributeBuilder
 * </p>
 * <p>
 * Description: 实现特殊的转化,BaseAttributeBuilder 子类应该提供一个空的构造函数，以及实现toObject或者toSql
 * </p>
 * <p>
 * Copyright: Copyright (c) 2020
 * </p>
 * <p>
 * Company/Department: DataPipeline
 * </p>
 *
 * @author wanglijun
 * @version 1.0
 * @since 2020-07-18 21:07
 */
public class BaseAttributeBuilder implements AttributePersistBuilder, AttributeSelectBuilder {


    /**
     * 默认实现
     *
     * @param sqlId
     * @param typeParameter
     * @param property
     * @return
     * @throws SQLException
     */
    static protected Object getDefaultValue(SQLManager sqlManager, String sqlId, TypeParameter typeParameter,
                                            PropertyDescriptor property) throws SQLException {
        BeanProcessor processor = getBeanProcessor(sqlManager, sqlId);
        Map<Class, JavaSqlTypeHandler> handlers = processor.getHandlers();
        Class propType = property.getPropertyType();
        JavaSqlTypeHandler handler = handlers.get(propType);
        if (handler == null) {
            handler = processor.getDefaultHandler();
        }
        Object value = handler.getValue(typeParameter);
        return value;
    }

    static public BeanProcessor getBeanProcessor(SQLManager sqlManager, String sqlId) {
        //这个代码与SQLScript代码重复
        BeanProcessor beanProcessor = (BeanProcessor) sqlManager.getProcessors().get(sqlId);
        if (beanProcessor != null) {
            return beanProcessor;
        }
        String ns = sqlId.substring(0, sqlId.indexOf(Separator.DOT));
        beanProcessor = (BeanProcessor) sqlManager.getProcessors().get(ns);
        if (beanProcessor != null) {
            return beanProcessor;
        } else {
            return sqlManager.getDefaultBeanProcessors();
        }


    }

    static public String getDefaultToSql(AbstractDialect dialect, String fieldName) {
        return fieldName;
    }

    /***
     * 生成SQL代码
     * @param dialect 数据库类型
     * @param fieldName 字段
     * @param colName 列表名
     * @param annotation 注解
     * @param tableDesc 表描述
     * @return
     */
    @Override
    public String toSql(AbstractDialect dialect, String fieldName, String colName, Annotation annotation, TableDesc tableDesc) {
        return null;
    }

    /***
     * 查询数据
     * @param sqlManager
     * @param an
     * @param sqlId
     * @param typeParameter
     * @param property
     * @return
     * @throws SQLException
     */
    @Override
    public Object toObject(SQLManager sqlManager, Annotation an, String sqlId, TypeParameter typeParameter, PropertyDescriptor property) throws SQLException {
        return null;
    }
}
